package com.rainkaze.IntelliDrawNote.fragment;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.rainkaze.IntelliDrawNote.R;
import com.rainkaze.IntelliDrawNote.adapter.TodoListAdapter;
import com.rainkaze.IntelliDrawNote.bean.TodoNote;
import com.rainkaze.IntelliDrawNote.db.MyDbHelper;

import java.util.List;


public class TodoListFragment extends Fragment {

    private RecyclerView listRv;
    private TodoListAdapter todoListAdapter;
    public MyDbHelper myDbHelper;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list_todo, container, false);
        listRv = view.findViewById(R.id.listRv);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Context context = requireContext();
        myDbHelper = new MyDbHelper(context);
        todoListAdapter = new TodoListAdapter(this);

        listRv.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        listRv.setAdapter(todoListAdapter);

        findAll();
    }

    public void findAll() {
        try {
            List<TodoNote> todoNoteList = myDbHelper.findAllTodo();
            if (todoListAdapter != null) {
                todoListAdapter.update(todoNoteList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(getContext(), "加载待办列表失败", Toast.LENGTH_SHORT).show();
        }
    }
}